/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.applis.demo.plugins;

//import opiam.admin.applis.demo.beans.Person; // if checks the modification without the AclsService
import opiam.admin.faare.persistence.javabeans.JBTop;
import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.service.services.acl.AclsService;
import opiam.admin.faare.struts.service.MenuFeaturePluginInterface;

import org.apache.log4j.Logger;


/**
 * This class allows to manage the display of the menus elements. 
 */
public class ShowMenusPlugin implements MenuFeaturePluginInterface
{
    /** Instance of the log4j logger.
     * Used to generate the execution traces. */
    private static Logger _logger =
        Logger.getLogger(ShowMenusPlugin.class.getName());

    /**
     * Indicates if the current menu element must be displayed.
     *
     * @param featureName  Name of the current menu element.
     * @param entry        Object declared in the menus_conf.xml.
     * @param userContext  User context. 
     *
     * @return true if the menu element is active, otherwise false.
     */
    @Override
	public boolean isActive(String featureName, JBTop entry,
                            UserContext userContext
                           )
    {
        _logger.info("featureName= " + featureName + "for user : " +
                     userContext.getDn()
                    );

        boolean isActive = false;

        if ((featureName.equals("templist")) || (featureName.equals("templistFull")))
        {
            if (userContext.getParameters().containsKey("cartIndic"))
            {
               isActive = true;
            }
        }

        if ((featureName.equals("editPerson")) || (featureName.equals("editPersonFull")))
        {
        	/*
        	// Without the AclsService
            String profile = userContext.getJbProfile().getName().trim().toLowerCase();
            if (entry instanceof Person)
            {
            	Person pers = (Person)entry;
            	String department = pers.getDepartment().toLowerCase();  
            	
            	if (((!profile.equals("directory administrator"))
            		&& (!profile.equals("service manager")))
    				&& ((department.equals("accounting") && (!profile.equals("accounting manager")))
    				|| (department.equals("human resources") && (!profile.equals("hr manager")))
    				|| (department.equals("product testing") && (!profile.equals("qa manager")))
    				|| (department.equals("product development") && (!profile.equals("pd manager")))))
    			{
            		isActive  = false;
    			}
            	else
            	{
            		isActive = true;
            	}
            }
            */
        	// Uses the AclsService
        	try 
			{
        		isActive = AclsService.isModificationEnabled(entry, userContext);
                if (!isActive)
                {
                    _logger.error("edit not done, pb with ACL");
                }
			}
            catch (Exception ex)
	        {
	            _logger.error(ex.getMessage());
	            _logger.debug("Trace", ex);
	        }
        }
        
        if (isActive)
        {
            _logger.info("featureName= " + featureName + " is active.");
        }
        else
        {
            _logger.info("featureName= " + featureName + " is not active.");
        }

        return isActive;
    }
}
